#pragma once

#include <iostream>
#include <functional>
#include <cstdio>

#define NUM 1024

void serviceIO(int sock)
{
    char buffer[NUM];
    while(true)
    {
        std::string msg;
        int n = read(sock, buffer, sizeof(buffer)-1);
        if(n > 0) //std::cout << clientip << "[" << clientport << "]" << msg << std::endl;
        {
            buffer[n] = 0;
            std::cout << buffer << std::endl;
        }
        else if(n == 0) break;//说明客户端退出了
        //服务端将客户端信息进行回显
        msg = buffer;
        msg += " server[echo]";
        write(sock, msg.c_str(), msg.size());
    }
}

class Task
{
private:
    using func_t = std::function<void(int)>;
public:
    Task(){}
    Task(int sock, func_t func)
    :_sock(sock), _func(func)
    {}
    void operator ()()
    {
        _func(_sock);
    } 
private:
    func_t _func;
    int _sock;
};